//
//  CancleVC.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/5/17.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CancleVC.h"
#import "CancelOrderTableViewCell.h"
@interface CancleVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSArray     *resonArray;
@property (nonatomic ,strong) NSMutableArray     *selectArray;
@end

@implementation CancleVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadCancleReson];
    //[self showNav];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self showShadow];
    self.resonArray=[NSArray array];
    self.selectArray=[NSMutableArray array];
    [self.view addSubview:[self tableView]];
    UIButton *MySureBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, MainH-49, MainW, 49)];
    [MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [MySureBtn setTitle:MyString(@"提交") forState:UIControlStateNormal];
    [MySureBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    [MySureBtn setBackgroundColor:Red_Color];
    [self.view addSubview:MySureBtn];
    // Do any additional setup after loading the view.
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, MainW, MainH-64-49)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.separatorStyle=0;
        _tableView.backgroundColor=[UIColor whiteColor];
        [_tableView registerNib:[UINib nibWithNibName:@"CancelOrderTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CancelOrderCell"];
    }
    return _tableView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 77;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.resonArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CancelOrderTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CancelOrderCell"];
    cell.title.text=[NSString stringWithFormat:@"%@",self.resonArray[indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.selectArray removeAllObjects];
    [self.selectArray addObject:self.resonArray[indexPath.row]];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]init];
    UIButton*cancle=[[UIButton alloc]initWithFrame:CGRectMake(MainW-30, 15, 20, 20)];
    [cancle setImage:[UIImage imageNamed:@"icon_cross"] forState:UIControlStateNormal];
    [cancle addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:cancle];
    MYLabel*title=[[MYLabel alloc]initWithFrame:CGRectMake(30, 15, MainW-60, 20)];
    title.text=MyString(@"取消订单");
    title.textColor=TextColor3;
    title.font=[UIFont systemFontOfSize:15];
    title.textAlignment=1;
    [view addSubview:title];
    MYLabel*title1=[[MYLabel alloc]initWithFrame:CGRectMake(10, title.bsg_bottom+15, MainW-60, 20)];
    title1.text=MyString(@"取消原因");
    title1.textColor=TextColor3;
    title1.font=[UIFont systemFontOfSize:15];
    [view addSubview:title1];
    return view;
}
-(void)submitClick{
    if (self.selectArray.count==0) {
        [self showToast:MyString(@"取消理由")];
        return;
    }else{
        [self loadCancleOrder];
    }
    
}
-(void)cancelClick{
    [self dismissViewControllerAnimated:YES completion:^{
        [self disShadow];
    }];
    //NSLog(@"取消");
}
-(void)loadCancleReson{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    [self doHttpRequestWithApi:API_CANCLEORDERRESON_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.resonArray=dic[@"data"];
        [self.tableView reloadData];
        
    }];
}

-(void)loadCancleOrder{
		
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"orderSn"]=self.orderSn;
    dic[@"reson"]=[self.selectArray mj_JSONString];
	
    [self doHttpRequestWithApi:API_CANCLEORDER_LIST method:POST dic:dic successBlock:^(NSDictionary *dic) {
		[self dismissDialog];
//		[self disShadow];
		if ([dic[@"status"]integerValue] == 0) {
			self.cancleBlock();
			[self dismissViewControllerAnimated:YES completion:^{
				[self disShadow];
			}];
		}

    }];
}
@end
